package com.imay.live.manager;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Handler;
import android.view.View;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.animated.base.AbstractAnimatedDrawable;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.imay.live.helper.EventHelper;
import com.imay.live.helper.ThreadManager;
import com.imay.tbas_api.IMYTBASObject;

import java.lang.reflect.Field;


/**
 * Created by nick on 2017/9/26.
 */

public class FirstRechargeManager {
    private IMYTBASObject rechargeSuccessNotify;
    private SimpleDraweeView mDraweeView;

    private static class FirstRechargeManagerHolder {
        private static final FirstRechargeManager INSTANCE = new FirstRechargeManager();
    }

    public static final FirstRechargeManager getInstance() {
        return FirstRechargeManagerHolder.INSTANCE;
    }

    private FirstRechargeManager() {
    }

    private Handler handler;
    private Runnable runnable;
    private  int time = 7000;
    private Animatable mAnimatable;


    public  void loadWebp(String url,SimpleDraweeView draweeView) {
        mDraweeView = draweeView;
        ControllerListener controllerListener = new BaseControllerListener() {
            @Override
            public void onFinalImageSet(String id, Object imageInfo, Animatable animatable) {
                if (animatable != null) {
                    mAnimatable = animatable;
                    try {
                        Field field = AbstractAnimatedDrawable.class.getDeclaredField("mLoopCount");
                        field.setAccessible(true);
                        field.set(animatable, 1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (animatable instanceof AbstractAnimatedDrawable){
                        int duration = ((AbstractAnimatedDrawable) animatable).getDuration();
                        time += duration;
                    }
                    animatable.start();
                    setRepeat();
                    handler.postDelayed(runnable, time);
                }
            }
        };
        PipelineDraweeControllerBuilder pipelineDraweeControllerBuilder = Fresco.newDraweeControllerBuilder();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).setResizeOptions(new ResizeOptions(80, 80)).build();
        pipelineDraweeControllerBuilder.setImageRequest(request);
        DraweeController controller = pipelineDraweeControllerBuilder.setOldController(draweeView.getController()).setAutoPlayAnimations(false).setControllerListener(controllerListener).build();
        draweeView.setController(controller);
    }
    private void setRepeat(){
        if (handler == null){
            handler = new Handler();
        }
        if (runnable == null){
            runnable = new Runnable() {

                @Override
                public void run() {
                    // handler自带方法实现定时器
                    try {
                        handler.postDelayed(this, time);
                        if (mAnimatable != null){
                            mAnimatable.start();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
        }
    }

    public void clearFirstrechargeButton(){
        if (mAnimatable != null){
            mAnimatable.stop();
        }
        if (mDraweeView!=null){
            mDraweeView.post(new Runnable() {
                @Override
                public void run() {
                    mDraweeView.setVisibility(View.GONE);
                }
            });
        }
        if (handler != null && runnable != null){
            handler.removeCallbacks(runnable);
            handler = null;
            runnable = null;
        }

    }

    /**
     * 充值成功通知
     */
    public void registerRechargeNotify() {
        rechargeSuccessNotify = new IMYTBASObject(true);
        rechargeSuccessNotify.stringCommand = EventHelper.EVENT_RECHARGE_SUCCESS_NOTIFY;
        rechargeSuccessNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject imytbasObject) {
                clearFirstrechargeButton();

            }
        });
        rechargeSuccessNotify.addEventListenWithTBASObject();
    }


}
